<template>
	<view class="content">
		<view class="order">
			<dl v-for="(item,index) in list" :key="index">
				<dt><image :src="item.url"></image></dt>
				<dd>
					<h2>{{item.pName}}</h2>
					<span>{{item.userIntended}}</span>
					<em>x{{item.num}}</em>
				</dd>
			</dl>
		</view>
		<view class="order-reserve">
			<li><text>姓名</text><input v-model="form.userName" placeholder="请填写收货人姓名" placeholder-class="placeholder" /></li>
			<li><text>手机号</text><input v-model="form.userMobile" placeholder="请填写收货手机号码" placeholder-class="placeholder" /></li>
			<li @tap='selectArea'><text>省市区</text><span>
				<em v-if="!selectText">请选择省市区</em><em class="now" v-else>{{areaText}}</em><icon class="icon-arrow"></icon></span>
			</li>
			<li><text>详细地址</text><input v-model="form.addrDetail" placeholder="请填写小区，楼栋门牌号" placeholder-class="placeholder" /></li>		 
		</view>
		<view class="fixed">
			<span>共计{{list.length}}件</span>
			<view class="but" @click="submit">
				提交订单
			</view>
		</view>
		
		<!--城市容器-->
		<lb-picker ref="picker" :list="area" v-model="selectText" level="3" mode="multiSelector"
			@change="handleChange"
			@confirm="handleConfirm"
			@cancel="handleCancel">
			<view slot="cancel-text">取消</view>
			<view slot="confirm-text">确定</view>
		</lb-picker>
		
		<!--城市联动插件地址 https://ext.dcloud.net.cn/plugin?id=1111-->
	
	</view>
</template>

<script>
	import LbPicker from '@/components/lb-picker'
	import areaData from '@/common/area-data-min'
	export default {
		components: {
			LbPicker
		},
		data() {
			return {
				list:[],
				area: areaData,
				selectText: '',
				areaText:'',
				form:{
					userName:'',
					userMobile:'',
					addrDetail:'',
					addrProvince:'',
					addrCity:'',
					addrArea:'',
				},
			}
		},
		onShow() {
			this.list = uni.getStorageSync('shopList')
			if(this.list.length===0){
				uni.navigateBack()
			}
		},
		methods: {
			selectArea() {
				this.$refs.picker.show()
			},
			handleChange (e) {
			  console.log('change::', e)
			},
			handleConfirm (e) {
			  // 如果存在多个picker，可以在picker上设置dataset属性，confirm中获取，就能区分是哪个picker了
			  console.log('confirm::', e)
			  if (e) {
			    const name = e.dataset.name
			    this.areaText = e.item.map(m => m.label).join('-')
			  				
				this.form.addrProvince = e.value[0]
				this.form.addrCity = e.value[1]
				this.form.addrArea = e.value[2]
			  }
			},
			handleCancel (e) {
			  console.log('cancel::', e)
			},
			submit(){
				const orderItemList = []
				this.list.forEach(item=>{
					orderItemList.push({
						pid:item.pid,
						num:item.num
					})
				})
				
				if(!this.form.userName){
					uni.showToast({
						icon:'error',
						title: '请输入姓名',
					});
					return
				}
				if(!this.form.userMobile){
					uni.showToast({
						icon:'error',
						title: '请输入手机号',
					});
					return
				}
				if(!this.form.addrDetail){
					uni.showToast({
						icon:'error',
						title: '请输入详细地址',
					});
					return
				}
				if(!this.form.addrProvince){
					uni.showToast({
						icon:'error',
						title: '请选择地址',
					});
					return
				}
				
				this.$api({
					url: `/order/commit`,
					method: "POST",
					data: {
						...this.form,
						orderItemList
					}
				}).then(res=>{
					uni.navigateTo({
						url:'../order/success'
					})
				})
			},
			
			navigateto(url) {
				wx.navigateTo({
					url: url
				})
			}
		}
	}
</script>

<style lang="scss">
	
	.content {
		padding-bottom: calc(env(safe-area-inset-bottom) + 120rpx);
	}
	
	.order {
		background: #fff;
		dl {
			display: flex;
			align-items: center;
			padding: 30rpx;
			dt {
				image {
					width: 152rpx;
					height: 152rpx;
				}
			}
			dd {
				flex: 1;
				position: relative;
				padding-left: 24rpx;
				h2 {
					height: 80rpx;
					font-size: 28rpx;
					overflow: hidden;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
				}
				span, em {
					display: block;
					font-size: 24rpx;
					margin: 10rpx 0 0;
					color: #999;
				}
				em {
					position: absolute;
					bottom: 0;
					right: 0;
				}
			}
		}
	}
	
	.order-reserve {
		padding: 0 30rpx;
		margin: 20rpx 0 0;
		background: #fff;
		li {
			display: flex;
			align-items: center;
			height: 92rpx;
			border-bottom: 2rpx solid #f1f1f1;
			&:last-of-type {
				border: none;
			}
			text {
				min-width: 180rpx;
				font-size: 28rpx;
			}
			input {
				flex: 1;
				font-size: 28rpx;
				text-align: right;
			}
			span {
				color: #999;
				display: flex;
				align-items: center;
				flex: 1;
				justify-content: flex-end;
				icon {
					display: flex;
					font-size: 26rpx;
					margin-left: 10rpx;
				}
				em {
					&.now {
						color: #333;
					}
				}
			}
			
		}
	}
	
	.placeholder {
		color: #999;
	}
	
	.fixed {
		width: 750rpx;
		display: flex;
		align-items: center;
		background: #fff;
		padding-bottom: calc(env(safe-area-inset-bottom));
		position: fixed;
		bottom: 0;
		left: 0;
		span {
			flex: 1;
			text-align: right;
			font-size: 28rpx;
			padding-right: 30rpx;
		}
		.but {
			width: 240rpx;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			font-size: 28rpx;
			background: #dc261b;
			color: #fff;
		}
	}
	
</style>
